from conan import ConanFile
from conan.tools.files import copy
from conan.tools.scm import Git
from conan.tools.files import load, update_conandata
from conan.tools.microsoft import vs_layout
from conan.tools.microsoft import MSBuild
from conan.tools.microsoft import VCVars
from conan.tools.microsoft import MSBuildDeps

import os

class InspItemOrientation(ConanFile):
    name = "inspitemfactory_orientation"

    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}

    default_options = {"shared": True, "fPIC": True}
    revision_mode = "scm"
    #generators = "MSBuildDeps"

    def set_version(self):
        git = Git(self)
        try:
            tag = git.run("describe --abbrev=0 --tags")
            self.version = tag 
        except Exception as e:
            self.version = "0.0.1"

    def generate(self):
        ms = VCVars(self)
        ms.generate()
        ms = MSBuildDeps(self)
        ms.generate()

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC
            
    def requirements(self):
        self.requires("commonuicontrol/[>=2.0 <2.1]")
        self.requires("inspectitems/0.0.1")
        self.requires("opencv/4.10.0")
        self.requires("hfvinspect4/1.1.4")

    def baseName(self):
        return "InspitemFactoryOrientation"

    def layout(self):
        vs_layout(self)
      
    def build(self):
        msbuild = MSBuild(self)
        slnDir = os.path.join(self.build_folder, "..", "sln", "InspItemFactoryOrientation.sln")
        msbuild.build(slnDir)  

    def package(self):
        arch = self.settings.get_safe("arch", default="x86")
        if arch == "x86":
            arch = "Win32"
        else:
            arch = "x64"

        build_type = self.settings.get_safe("build_type", default="Release")

        arch_build_type = "%s_%s"%(arch, build_type)

        copy(self, pattern="*.h", src=self.source_folder, dst=os.path.join(self.package_folder, "include"), keep_path=True, excludes=["VersionMacro.h","stdafx.h", "Resource.h", "pch.h"])
        copy(self, pattern="*.lib", src=os.path.join(self.build_folder, arch_build_type), dst=os.path.join(self.package_folder, "lib"), keep_path=False)
        copy(self, pattern="*.dll", src=os.path.join(self.build_folder, arch_build_type), dst=os.path.join(self.package_folder, "bin"), keep_path=False)
        copy(self, pattern="*.PDB", src=os.path.join(self.build_folder, arch_build_type), dst=os.path.join(self.package_folder, "pdb"), keep_path=False)

    def package_info(self):
        pass
